package net.dfrz.factory;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class DaoFactory {
	
	private static Properties prop = new Properties();
	private DaoFactory(){
		try{
			InputStream in = DaoFactory.class.getClassLoader().getResourceAsStream("net/dfrz/factory/dao.properties");
			prop.load(in);
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	};
	private static final DaoFactory instance = new DaoFactory();
	
	private static Map<String, Object> daoMap = new HashMap<String, Object>();
	
	public static DaoFactory getInstance(){
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	public <T> T createDao(Class<T> interfaceClass){
		try{
			String key = interfaceClass.getSimpleName();
			if(daoMap.get(key)==null){
				String className = prop.getProperty(key);
				daoMap.put(key, Class.forName(className).newInstance());
			}
			return (T) daoMap.get(key);
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
}
